<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>MySQL数据类型精讲 | 知识库</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/noteslibrary/img/favicon.ico">
    <meta name="description" content="Java后端技术博客,专注Java后端学习与总结。HTML,CSS,JavaScript,TypeScript,JavaSE,Python,Vue,React,Node,Git,Github等技术文章。">
    <meta name="keywords" content="后端博客,个人技术博客,后端,后端开发,后端框架,后端面试题,技术文档,学习,面试,HTML,CSS,JavaScript,TypeScript,JavaSE,Python,Vue,React,Node,Git,Github">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/noteslibrary/assets/css/0.styles.2500ff6d.css" as="style"><link rel="preload" href="/noteslibrary/assets/js/app.dc0c3d24.js" as="script"><link rel="preload" href="/noteslibrary/assets/js/2.a664539c.js" as="script"><link rel="preload" href="/noteslibrary/assets/js/99.e024f354.js" as="script"><link rel="prefetch" href="/noteslibrary/assets/js/10.bab1a8b9.js"><link rel="prefetch" href="/noteslibrary/assets/js/100.245d2d1d.js"><link rel="prefetch" href="/noteslibrary/assets/js/101.9d1c6c13.js"><link rel="prefetch" href="/noteslibrary/assets/js/102.d187686f.js"><link rel="prefetch" href="/noteslibrary/assets/js/103.b0e7acf6.js"><link rel="prefetch" href="/noteslibrary/assets/js/104.895c9e53.js"><link rel="prefetch" href="/noteslibrary/assets/js/105.235036be.js"><link rel="prefetch" href="/noteslibrary/assets/js/106.17a71ae1.js"><link rel="prefetch" href="/noteslibrary/assets/js/107.256ceb5d.js"><link rel="prefetch" href="/noteslibrary/assets/js/108.57832d85.js"><link rel="prefetch" href="/noteslibrary/assets/js/109.f8b1d1c7.js"><link rel="prefetch" href="/noteslibrary/assets/js/11.2c69801a.js"><link rel="prefetch" href="/noteslibrary/assets/js/110.f3ce6a68.js"><link rel="prefetch" href="/noteslibrary/assets/js/111.bdfd28f4.js"><link rel="prefetch" href="/noteslibrary/assets/js/112.878f599c.js"><link rel="prefetch" href="/noteslibrary/assets/js/113.0efa07cf.js"><link rel="prefetch" href="/noteslibrary/assets/js/114.c7aba251.js"><link rel="prefetch" href="/noteslibrary/assets/js/115.8b2e628d.js"><link rel="prefetch" href="/noteslibrary/assets/js/116.3392d8ba.js"><link rel="prefetch" href="/noteslibrary/assets/js/117.310538d0.js"><link rel="prefetch" href="/noteslibrary/assets/js/118.81eb015b.js"><link rel="prefetch" href="/noteslibrary/assets/js/12.e77f5a18.js"><link rel="prefetch" href="/noteslibrary/assets/js/13.d24d0054.js"><link rel="prefetch" href="/noteslibrary/assets/js/14.f29d4d33.js"><link rel="prefetch" href="/noteslibrary/assets/js/15.8694e081.js"><link rel="prefetch" href="/noteslibrary/assets/js/16.03ffb143.js"><link rel="prefetch" href="/noteslibrary/assets/js/17.83edd7b9.js"><link rel="prefetch" href="/noteslibrary/assets/js/18.58ca4447.js"><link rel="prefetch" href="/noteslibrary/assets/js/19.e136d5c2.js"><link rel="prefetch" href="/noteslibrary/assets/js/20.fd3961b6.js"><link rel="prefetch" href="/noteslibrary/assets/js/21.207406c6.js"><link rel="prefetch" href="/noteslibrary/assets/js/22.b8738ce2.js"><link rel="prefetch" href="/noteslibrary/assets/js/23.33e4529d.js"><link rel="prefetch" href="/noteslibrary/assets/js/24.ab5493c5.js"><link rel="prefetch" href="/noteslibrary/assets/js/25.2506ce48.js"><link rel="prefetch" href="/noteslibrary/assets/js/26.7e6a9c14.js"><link rel="prefetch" href="/noteslibrary/assets/js/27.e7b4e92a.js"><link rel="prefetch" href="/noteslibrary/assets/js/28.7ad46ba6.js"><link rel="prefetch" href="/noteslibrary/assets/js/29.81666f41.js"><link rel="prefetch" href="/noteslibrary/assets/js/3.8f13cd17.js"><link rel="prefetch" href="/noteslibrary/assets/js/30.07ada09b.js"><link rel="prefetch" href="/noteslibrary/assets/js/31.f271c8cf.js"><link rel="prefetch" href="/noteslibrary/assets/js/32.2350914c.js"><link rel="prefetch" href="/noteslibrary/assets/js/33.9d7bfb77.js"><link rel="prefetch" href="/noteslibrary/assets/js/34.6fcf6f6b.js"><link rel="prefetch" href="/noteslibrary/assets/js/35.0c3a88fb.js"><link rel="prefetch" href="/noteslibrary/assets/js/36.574ca92a.js"><link rel="prefetch" href="/noteslibrary/assets/js/37.72e38074.js"><link rel="prefetch" href="/noteslibrary/assets/js/38.16d408fb.js"><link rel="prefetch" href="/noteslibrary/assets/js/39.63abc4a8.js"><link rel="prefetch" href="/noteslibrary/assets/js/4.3e68fc94.js"><link rel="prefetch" href="/noteslibrary/assets/js/40.ca2bdd48.js"><link rel="prefetch" href="/noteslibrary/assets/js/41.f82c80ec.js"><link rel="prefetch" href="/noteslibrary/assets/js/42.d1b8f579.js"><link rel="prefetch" href="/noteslibrary/assets/js/43.d3fd6260.js"><link rel="prefetch" href="/noteslibrary/assets/js/44.491f9afa.js"><link rel="prefetch" href="/noteslibrary/assets/js/45.1ee0d084.js"><link rel="prefetch" href="/noteslibrary/assets/js/46.a3e0be1f.js"><link rel="prefetch" href="/noteslibrary/assets/js/47.647908d1.js"><link rel="prefetch" href="/noteslibrary/assets/js/48.9658b8b0.js"><link rel="prefetch" href="/noteslibrary/assets/js/49.8f4b8327.js"><link rel="prefetch" href="/noteslibrary/assets/js/5.a0a97ccd.js"><link rel="prefetch" href="/noteslibrary/assets/js/50.f46d1433.js"><link rel="prefetch" href="/noteslibrary/assets/js/51.f1b784a0.js"><link rel="prefetch" href="/noteslibrary/assets/js/52.cce45956.js"><link rel="prefetch" href="/noteslibrary/assets/js/53.d66dabe3.js"><link rel="prefetch" href="/noteslibrary/assets/js/54.384b864b.js"><link rel="prefetch" href="/noteslibrary/assets/js/55.47ed19f2.js"><link rel="prefetch" href="/noteslibrary/assets/js/56.193cd456.js"><link rel="prefetch" href="/noteslibrary/assets/js/57.e6ea1f8c.js"><link rel="prefetch" href="/noteslibrary/assets/js/58.97fd2330.js"><link rel="prefetch" href="/noteslibrary/assets/js/59.b0c3d9ea.js"><link rel="prefetch" href="/noteslibrary/assets/js/6.50cbd75f.js"><link rel="prefetch" href="/noteslibrary/assets/js/60.d01d0651.js"><link rel="prefetch" href="/noteslibrary/assets/js/61.385e9bae.js"><link rel="prefetch" href="/noteslibrary/assets/js/62.a93fa4c8.js"><link rel="prefetch" href="/noteslibrary/assets/js/63.f72a2142.js"><link rel="prefetch" href="/noteslibrary/assets/js/64.3bf0b024.js"><link rel="prefetch" href="/noteslibrary/assets/js/65.cb1cb3bb.js"><link rel="prefetch" href="/noteslibrary/assets/js/66.4c9ff8cd.js"><link rel="prefetch" href="/noteslibrary/assets/js/67.2fc17900.js"><link rel="prefetch" href="/noteslibrary/assets/js/68.fd3ee410.js"><link rel="prefetch" href="/noteslibrary/assets/js/69.682be05d.js"><link rel="prefetch" href="/noteslibrary/assets/js/7.80203dee.js"><link rel="prefetch" href="/noteslibrary/assets/js/70.29428a45.js"><link rel="prefetch" href="/noteslibrary/assets/js/71.aff6ef6b.js"><link rel="prefetch" href="/noteslibrary/assets/js/72.fe7572e0.js"><link rel="prefetch" href="/noteslibrary/assets/js/73.e52bc1a7.js"><link rel="prefetch" href="/noteslibrary/assets/js/74.5e72ee84.js"><link rel="prefetch" href="/noteslibrary/assets/js/75.5a5bba64.js"><link rel="prefetch" href="/noteslibrary/assets/js/76.70d6ff7c.js"><link rel="prefetch" href="/noteslibrary/assets/js/77.1db1c302.js"><link rel="prefetch" href="/noteslibrary/assets/js/78.137c92e8.js"><link rel="prefetch" href="/noteslibrary/assets/js/79.8455d34b.js"><link rel="prefetch" href="/noteslibrary/assets/js/8.9e13e493.js"><link rel="prefetch" href="/noteslibrary/assets/js/80.127f3a4e.js"><link rel="prefetch" href="/noteslibrary/assets/js/81.191d3614.js"><link rel="prefetch" href="/noteslibrary/assets/js/82.6ae31745.js"><link rel="prefetch" href="/noteslibrary/assets/js/83.640cd3d7.js"><link rel="prefetch" href="/noteslibrary/assets/js/84.22c036e2.js"><link rel="prefetch" href="/noteslibrary/assets/js/85.692cd496.js"><link rel="prefetch" href="/noteslibrary/assets/js/86.793e38a7.js"><link rel="prefetch" href="/noteslibrary/assets/js/87.eb27f1d6.js"><link rel="prefetch" href="/noteslibrary/assets/js/88.6d48c75e.js"><link rel="prefetch" href="/noteslibrary/assets/js/89.a798bacb.js"><link rel="prefetch" href="/noteslibrary/assets/js/9.7e864ff7.js"><link rel="prefetch" href="/noteslibrary/assets/js/90.e814aadc.js"><link rel="prefetch" href="/noteslibrary/assets/js/91.5c132772.js"><link rel="prefetch" href="/noteslibrary/assets/js/92.d0d1984d.js"><link rel="prefetch" href="/noteslibrary/assets/js/93.bbc81ca6.js"><link rel="prefetch" href="/noteslibrary/assets/js/94.242fbc29.js"><link rel="prefetch" href="/noteslibrary/assets/js/95.f7232d68.js"><link rel="prefetch" href="/noteslibrary/assets/js/96.440e6bbe.js"><link rel="prefetch" href="/noteslibrary/assets/js/97.a8ad3aa8.js"><link rel="prefetch" href="/noteslibrary/assets/js/98.e9a01f98.js">
    <link rel="stylesheet" href="/noteslibrary/assets/css/0.styles.2500ff6d.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/noteslibrary/" class="home-link router-link-active"><img src="/noteslibrary/img/EB-logo.png" alt="知识库" class="logo"> <span class="site-name can-hide">知识库</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/noteslibrary/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="基础" class="dropdown-title"><a href="/noteslibrary/basis/" class="link-title">基础</a> <span class="title" style="display:none;">基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《计算机网络》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《计算机组成原理》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《操作系统》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《数据结构》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《算法》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《设计模式》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/noteslibrary/fontend/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/0796ba76b4b55368.html" class="nav-link">《JS笔记》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/f344d070a1031ef7.html" class="nav-link">《ES6笔记》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/note/vue/" class="nav-link">《Vue笔记》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><a href="/noteslibrary/backend/" class="link-title">后端</a> <span class="title" style="display:none;">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>JavaEE框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/d0b17d/" class="nav-link">《DAO全家桶》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/a9ac80/" class="nav-link">《Spring全家桶》</a></li></ul></li><li class="dropdown-item"><h4>BigData框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/9515c0/404" class="nav-link">《Hadoop生态》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9515c0/404" class="nav-link">《Spark生态》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9515c0/404" class="nav-link">《Flink生态》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><a href="/noteslibrary/database/" class="link-title">数据库</a> <span class="title" style="display:none;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/32e9fd/" class="nav-link">MySQL</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="工具" class="dropdown-title"><a href="/noteslibrary/tools/" class="link-title">工具</a> <span class="title" style="display:none;">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/3ac065/" class="nav-link">Maven</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/5760de/" class="nav-link">Git</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="收藏" class="dropdown-title"><a href="/noteslibrary/collect/" class="link-title">收藏</a> <span class="title" style="display:none;">收藏</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/50c072/" class="nav-link">博客收藏</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其他" class="dropdown-title"><a href="/noteslibrary/others/" class="link-title">其他</a> <span class="title" style="display:none;">其他</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>博客部署</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/db737d/" class="nav-link">Hexo</a></li></ul></li><li class="dropdown-item"><h4>系统安装</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/3ac066/" class="nav-link">系统安装</a></li></ul></li><li class="dropdown-item"><h4>idea快捷键</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/111c73/" class="nav-link">idea快捷键</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/noteslibrary/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/noteslibrary/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/noteslibrary/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/noteslibrary/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/lcfqzd/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/noteslibrary/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="基础" class="dropdown-title"><a href="/noteslibrary/basis/" class="link-title">基础</a> <span class="title" style="display:none;">基础</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《计算机网络》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《计算机组成原理》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《操作系统》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《数据结构》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《算法》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9a7ee40fc232253e/" class="nav-link">《设计模式》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/noteslibrary/fontend/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/0796ba76b4b55368.html" class="nav-link">《JS笔记》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/f344d070a1031ef7.html" class="nav-link">《ES6笔记》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/note/vue/" class="nav-link">《Vue笔记》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><a href="/noteslibrary/backend/" class="link-title">后端</a> <span class="title" style="display:none;">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>JavaEE框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/d0b17d/" class="nav-link">《DAO全家桶》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/a9ac80/" class="nav-link">《Spring全家桶》</a></li></ul></li><li class="dropdown-item"><h4>BigData框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/9515c0/404" class="nav-link">《Hadoop生态》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9515c0/404" class="nav-link">《Spark生态》</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/9515c0/404" class="nav-link">《Flink生态》</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><a href="/noteslibrary/database/" class="link-title">数据库</a> <span class="title" style="display:none;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/32e9fd/" class="nav-link">MySQL</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="工具" class="dropdown-title"><a href="/noteslibrary/tools/" class="link-title">工具</a> <span class="title" style="display:none;">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/3ac065/" class="nav-link">Maven</a></li><li class="dropdown-subitem"><a href="/noteslibrary/pages/5760de/" class="nav-link">Git</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="收藏" class="dropdown-title"><a href="/noteslibrary/collect/" class="link-title">收藏</a> <span class="title" style="display:none;">收藏</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4></h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/50c072/" class="nav-link">博客收藏</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其他" class="dropdown-title"><a href="/noteslibrary/others/" class="link-title">其他</a> <span class="title" style="display:none;">其他</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>博客部署</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/db737d/" class="nav-link">Hexo</a></li></ul></li><li class="dropdown-item"><h4>系统安装</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/3ac066/" class="nav-link">系统安装</a></li></ul></li><li class="dropdown-item"><h4>idea快捷键</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/noteslibrary/pages/111c73/" class="nav-link">idea快捷键</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/noteslibrary/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/noteslibrary/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/noteslibrary/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/noteslibrary/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/lcfqzd/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>MySQL笔记</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/noteslibrary/pages/32e9fd/" class="sidebar-link">写在前面</a></li><li><a href="/noteslibrary/pages/cfda94/" class="sidebar-link">数据库概述</a></li><li><a href="/noteslibrary/pages/497bcb/" class="sidebar-link">MySQL环境搭建</a></li><li><a href="/noteslibrary/pages/9c9f3a/" class="sidebar-link">基本的SELECT语句</a></li><li><a href="/noteslibrary/pages/b59830/" class="sidebar-link">运算符</a></li><li><a href="/noteslibrary/pages/6fef5b/" class="sidebar-link">排序与分页</a></li><li><a href="/noteslibrary/pages/3a50a8/" class="sidebar-link">多表查询</a></li><li><a href="/noteslibrary/pages/8de5b4/" class="sidebar-link">单行函数</a></li><li><a href="/noteslibrary/pages/2dfe6d/" class="sidebar-link">聚合函数</a></li><li><a href="/noteslibrary/pages/3ea5ac/" class="sidebar-link">子查询</a></li><li><a href="/noteslibrary/pages/70a90b/" class="sidebar-link">创建和管理表</a></li><li><a href="/noteslibrary/pages/a20c5b/" class="sidebar-link">数据处理之增删改</a></li><li><a href="/noteslibrary/pages/9515c0/" aria-current="page" class="active sidebar-link">MySQL数据类型精讲</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_1-mysql中的数据类型" class="sidebar-link">1. MySQL中的数据类型</a></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_2-整数类型" class="sidebar-link">2. 整数类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_2-1-类型介绍" class="sidebar-link">2.1 类型介绍</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_2-2-可选属性" class="sidebar-link">2.2 可选属性</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_2-3-适用场景" class="sidebar-link">2.3 适用场景</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_2-4-如何选择" class="sidebar-link">2.4 如何选择？</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_3-浮点类型" class="sidebar-link">3. 浮点类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_3-1-类型介绍" class="sidebar-link">3.1 类型介绍</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_3-2-数据精度说明" class="sidebar-link">3.2 数据精度说明</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_3-3-精度误差说明" class="sidebar-link">3.3 精度误差说明</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_4-定点数类型" class="sidebar-link">4. 定点数类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_4-1-类型介绍" class="sidebar-link">4.1 类型介绍</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_4-2-开发中经验" class="sidebar-link">4.2 开发中经验</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_5-位类型-bit" class="sidebar-link">5. 位类型：BIT</a></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_6-日期与时间类型" class="sidebar-link">6. 日期与时间类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_6-1-year类型" class="sidebar-link">6.1 YEAR类型</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_6-2-date类型" class="sidebar-link">6.2 DATE类型</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_6-3-time类型" class="sidebar-link">6.3 TIME类型</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_6-4-datetime类型" class="sidebar-link">6.4 DATETIME类型</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_6-5-timestamp类型" class="sidebar-link">6.5 TIMESTAMP类型</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_6-6-开发中经验" class="sidebar-link">6.6 开发中经验</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_7-文本字符串类型" class="sidebar-link">7. 文本字符串类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_7-1-char与varchar类型" class="sidebar-link">7.1 CHAR与VARCHAR类型</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#_7-2-text类型" class="sidebar-link">7.2 TEXT类型</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_8-enum类型" class="sidebar-link">8. ENUM类型</a></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_9-set类型" class="sidebar-link">9. SET类型</a></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_10-二进制字符串类型" class="sidebar-link">10. 二进制字符串类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#binary与varbinary类型" class="sidebar-link">BINARY与VARBINARY类型</a></li><li class="sidebar-sub-header level3"><a href="/noteslibrary/pages/9515c0/#blob类型" class="sidebar-link">BLOB类型</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_11-json-类型" class="sidebar-link">11. JSON 类型</a></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_12-空间类型" class="sidebar-link">12. 空间类型</a></li><li class="sidebar-sub-header level2"><a href="/noteslibrary/pages/9515c0/#_13-小结及选择建议" class="sidebar-link">13. 小结及选择建议</a></li></ul></li><li><a href="/noteslibrary/pages/98e681/" class="sidebar-link">约束</a></li><li><a href="/noteslibrary/pages/4535e0/" class="sidebar-link">视图</a></li><li><a href="/noteslibrary/pages/e5e604/" class="sidebar-link">存储过程与函数</a></li><li><a href="/noteslibrary/pages/f68b46/" class="sidebar-link">变量与流程控制与游标</a></li><li><a href="/noteslibrary/pages/fe941e/" class="sidebar-link">触发器</a></li><li><a href="/noteslibrary/pages/efad7b/" class="sidebar-link">MySQL8其它新特性</a></li></ul></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/noteslibrary/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/noteslibrary/database/#数据库" data-v-06225672>数据库</a></li><li data-v-06225672><a href="/noteslibrary/database/#MySQL笔记" data-v-06225672>MySQL笔记</a></li></ul> <div class="info" data-v-06225672><!----> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2023-04-05</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">MySQL数据类型精讲<!----></h1> <!----> <div class="theme-vdoing-content content__default"><h1 id="mysql数据类型精讲"><a href="#mysql数据类型精讲" class="header-anchor">#</a> MySQL数据类型精讲</h1> <h2 id="_1-mysql中的数据类型"><a href="#_1-mysql中的数据类型" class="header-anchor">#</a> 1. MySQL中的数据类型</h2> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929199.png" alt="image-20220610151956022"></p> <p>​       常见数据类型的属性，如下：</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929200.png" alt="image-20220610152019199"></p> <h2 id="_2-整数类型"><a href="#_2-整数类型" class="header-anchor">#</a> 2. 整数类型</h2> <h3 id="_2-1-类型介绍"><a href="#_2-1-类型介绍" class="header-anchor">#</a> 2.1 类型介绍</h3> <p>整数类型一共有 5 种，包括 <code>TINYINT</code>、<code>SMALLINT</code>、<code>MEDIUMINT</code>、<code>INT</code>（INTEGER）和 <code>BIGINT</code>。
它们的区别如下表所示：</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929201.png" alt="image-20220610152113850"></p> <h3 id="_2-2-可选属性"><a href="#_2-2-可选属性" class="header-anchor">#</a> 2.2 可选属性</h3> <p><strong>整数类型的可选属性有三个：</strong></p> <h4 id="_2-2-1-m"><a href="#_2-2-1-m" class="header-anchor">#</a> 2.2.1 M</h4> <p><code>M</code> : 表示显示宽度，M的取值范围是(0, 255)。例如，int(5)：当数据宽度小于5位的时候在数字前面需要用
字符填满宽度。该项功能需要配合“ <code>ZEROFILL</code> ”使用，表示用“0”填满宽度，否则指定显示宽度无效。</p> <p>如果设置了显示宽度，那么插入的数据宽度超过显示宽度限制，会不会截断或插入失败？</p> <p>答案：不会对插入的数据有任何影响，还是按照类型的实际宽度进行保存，即 <code>显示宽度与类型可以存储的 值范围无关</code> 。<strong>从MySQL 8.0.17开始，整数数据类型不推荐使用显示宽度属性。</strong></p> <p>整型数据类型可以在定义表结构时指定所需要的显示宽度，如果不指定，则系统为每一种类型指定默认
的宽度值。</p> <p>举例：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_int1 <span class="token punctuation">(</span> x <span class="token keyword">TINYINT</span><span class="token punctuation">,</span>　y <span class="token keyword">SMALLINT</span><span class="token punctuation">,</span>　z <span class="token keyword">MEDIUMINT</span><span class="token punctuation">,</span>　m <span class="token keyword">INT</span><span class="token punctuation">,</span>　n <span class="token keyword">BIGINT</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>查看表结构 （MySQL5.7中显式如下，MySQL8中不再显式范围）</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">desc</span> test_int1<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">-------+--------------+------+-----+---------+-------+</span>
<span class="token operator">|</span> Field <span class="token operator">|</span> <span class="token keyword">Type</span>         <span class="token operator">|</span> <span class="token boolean">Null</span> <span class="token operator">|</span> <span class="token keyword">Key</span> <span class="token operator">|</span> <span class="token keyword">Default</span> <span class="token operator">|</span> Extra <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">-------+--------------+------+-----+---------+-------+</span>
<span class="token operator">|</span>  x    <span class="token operator">|</span> <span class="token keyword">tinyint</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>   <span class="token operator">|</span> YES <span class="token operator">|</span>   <span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span>       <span class="token operator">|</span>
<span class="token operator">|</span> 　y   <span class="token operator">|</span> <span class="token keyword">smallint</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span>  <span class="token operator">|</span> YES <span class="token operator">|</span>   <span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span>    	 <span class="token operator">|</span>
<span class="token operator">|</span> 　z   <span class="token operator">|</span> <span class="token keyword">mediumint</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">)</span> <span class="token operator">|</span> YES <span class="token operator">|</span>   <span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span>     	 <span class="token operator">|</span>
<span class="token operator">|</span> 　m   <span class="token operator">|</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span>      <span class="token operator">|</span> YES <span class="token operator">|</span>   <span class="token operator">|</span> <span class="token boolean">NULL</span>  	 <span class="token operator">|</span>     	 <span class="token operator">|</span>
<span class="token operator">|</span> 　n   <span class="token operator">|</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span>   <span class="token operator">|</span> YES <span class="token operator">|</span>   <span class="token operator">|</span> <span class="token boolean">NULL</span> 	     <span class="token operator">|</span>   	 <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">-------+--------------+------+-----+---------+-------+</span>
<span class="token number">5</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>TINYINT有符号数和无符号数的取值范围分别为-128~127和0~255，由于负号占了一个数字位，因此
TINYINT默认的显示宽度为4。同理，其他整数类型的默认显示宽度与其有符号数的最小值的宽度相同。</p> <p>举例：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_int2<span class="token punctuation">(</span>
    f1 <span class="token keyword">INT</span><span class="token punctuation">,</span>
    f2 <span class="token keyword">INT</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    f3 <span class="token keyword">INT</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> ZEROFILL
<span class="token punctuation">)</span>

<span class="token keyword">DESC</span> test_int2<span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_int2<span class="token punctuation">(</span>f1<span class="token punctuation">,</span>f2<span class="token punctuation">,</span>f3<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">123</span><span class="token punctuation">,</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_int2<span class="token punctuation">(</span>f1<span class="token punctuation">,</span>f2<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">123456</span><span class="token punctuation">,</span><span class="token number">123456</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_int2<span class="token punctuation">(</span>f1<span class="token punctuation">,</span>f2<span class="token punctuation">,</span>f3<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">123456</span><span class="token punctuation">,</span><span class="token number">123456</span><span class="token punctuation">,</span><span class="token number">123456</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><div class="language- line-numbers-mode"><pre class="language-text"><code>mysql&gt; SELECT * FROM test_int2;
+--------+--------+--------+
| f1     | f2     | f3     |
+--------+--------+--------+
|   1    |   123  |  00123 |
| 123456 | 123456 |  NULL  |
| 123456 | 123456 | 123456 |
+--------+--------+--------+
3 rows in set (0.00 sec)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h4 id="_2-2-2-unsigned"><a href="#_2-2-2-unsigned" class="header-anchor">#</a> 2.2.2 UNSIGNED</h4> <p><code>UNSIGNED</code> : 无符号类型（非负），所有的整数类型都有一个可选的属性UNSIGNED（无符号属性），无符号整数类型的最小取值为0。所以，如果需要在MySQL数据库中保存非负整数值时，可以将整数类型置为无符号类型。</p> <p>int类型默认显示宽度为int(11)，无符号int类型默认显示宽度为int(10)。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_int3<span class="token punctuation">(</span>
f1 <span class="token keyword">INT</span> <span class="token keyword">UNSIGNED</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
mysql<span class="token operator">&gt;</span> <span class="token keyword">desc</span> test_int3<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">-------+------------------+------+-----+---------+-------+</span>
<span class="token operator">|</span> Field <span class="token operator">|</span> <span class="token keyword">Type</span>             <span class="token operator">|</span> <span class="token boolean">Null</span> <span class="token operator">|</span> <span class="token keyword">Key</span> <span class="token operator">|</span> <span class="token keyword">Default</span> <span class="token operator">|</span> Extra <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">-------+------------------+------+-----+---------+-------+</span>
<span class="token operator">|</span> f1    <span class="token operator">|</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token keyword">unsigned</span> <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span>       <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">-------+------------------+------+-----+---------+-------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h4 id="_2-2-3-zerofill"><a href="#_2-2-3-zerofill" class="header-anchor">#</a> 2.2.3 ZEROFILL</h4> <p><code>ZEROFILL</code> : 0填充,（如果某列是ZEROFILL，那么MySQL会自动为当前列添加UNSIGNED属性），如果指定了ZEROFILL只是表示不够M位时，用0在左边填充，如果超过M位，只要不超过数据存储范围即可。</p> <p>原来，在 int(M) 中，M 的值跟 int(M) 所占多少存储空间并无任何关系。 int(3)、int(4)、int(8) 在磁盘上都是占用 4 bytes 的存储空间。也就是说，**int(M)，必须和UNSIGNED ZEROFILL一起使用才有意义。**如果整数值超过M位，就按照实际位数存储。只是无须再用字符 0 进行填充。</p> <h3 id="_2-3-适用场景"><a href="#_2-3-适用场景" class="header-anchor">#</a> 2.3 适用场景</h3> <p><code>TINYINT</code> ：一般用于枚举数据，比如系统设定取值范围很小且固定的场景。
<code>SMALLINT</code> ：可以用于较小范围的统计数据，比如统计工厂的固定资产库存数量等。
<code>MEDIUMINT</code> ：用于较大整数的计算，比如车站每日的客流量等。
<code>INT、INTEGER</code> ：取值范围足够大，一般情况下不用考虑超限问题，用得最多。比如商品编号。
<code>BIGINT</code> ：只有当你处理特别巨大的整数时才会用到。比如双十一的交易量、大型门户网站点击量、证券公司衍生产品持仓等。</p> <h3 id="_2-4-如何选择"><a href="#_2-4-如何选择" class="header-anchor">#</a> 2.4 如何选择？</h3> <p>在评估用哪种整数类型的时候，你需要考虑 <code>存储空间</code> 和 <code>可靠性</code> 的平衡问题：一方 面，用占用字节数少的整数类型可以节省存储空间；另一方面，要是为了节省存储空间， 使用的整数类型取值范围太小，一旦遇到超出取值范围的情况，就可能引起 <code>系统错误</code> ，影响可靠性。</p> <p>举个例子，商品编号采用的数据类型是 INT。原因就在于，客户门店中流通的商品种类较多，而且，每天都有旧商品下架，新商品上架，这样不断迭代，日积月累。</p> <p>如果使用 SMALLINT 类型，虽然占用字节数比 INT 类型的整数少，但是却不能保证数据不会超出范围65535。相反，使用 INT，就能确保有足够大的取值范围，不用担心数据超出范围影响可靠性的问题。</p> <p>你要注意的是，在实际工作中，<strong>系统故障产生的成本远远超过增加几个字段存储空间所产生的成本</strong>。因此，我建议你首先确保数据不会超过取值范围，在这个前提之下，再去考虑如何节省存储空间。</p> <h2 id="_3-浮点类型"><a href="#_3-浮点类型" class="header-anchor">#</a> 3. 浮点类型</h2> <h3 id="_3-1-类型介绍"><a href="#_3-1-类型介绍" class="header-anchor">#</a> 3.1 类型介绍</h3> <p>浮点数和定点数类型的特点是可以 <code>处理小数</code> ，你可以把整数看成小数的一个特例。因此，浮点数和定点数的使用场景，比整数大多了。 MySQL支持的浮点数类型，分别是 FLOAT、DOUBLE、REAL。</p> <ul><li>FLOAT 表示单精度浮点数；</li> <li>DOUBLE 表示双精度浮点数；</li></ul> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929202.png" alt="image-20220610153358591"></p> <ul><li>REAL默认就是 DOUBLE。如果你把 SQL 模式设定为启用“ REAL_AS_FLOAT ”，那 么，MySQL 就认为REAL 是 FLOAT。如果要启用“REAL_AS_FLOAT”，可以通过以下 SQL 语句实现：</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">SET</span> sql_mode <span class="token operator">=</span> “REAL_AS_FLOAT”<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>问题1</strong>：FLOAT 和 DOUBLE 这两种数据类型的区别是啥呢？</p> <p>FLOAT 占用字节数少，取值范围小；DOUBLE 占用字节数多，取值范围也大。</p> <p><strong>问题2</strong>：为什么浮点数类型的无符号数取值范围，只相当于有符号数取值范围的一半，也就是只相当于有符号数取值范围大于等于零的部分呢？</p> <p>MySQL 存储浮点数的格式为： 符号(S) 、 尾数(M) 和  阶码(E) 。因此，无论有没有符号，MySQL 的浮点数都会存储表示符号的部分。因此， 所谓的无符号数取值范围，其实就是有符号数取值范围大于等于零的部分。</p> <h3 id="_3-2-数据精度说明"><a href="#_3-2-数据精度说明" class="header-anchor">#</a> 3.2 数据精度说明</h3> <p>对于浮点类型，在MySQL中单精度值使用 <code>4</code> 个字节，双精度值使用 <code>8</code> 个字节。</p> <ul><li><p>MySQL允许使用 非标准语法 （其他数据库未必支持，因此如果涉及到数据迁移，则最好不要这么用）： <code>FLOAT(M,D)</code> 或 <code>DOUBLE(M,D)</code> 。这里，M称为 <code>精度</code> ，D称为 <code>标度</code>。(M,D)中 M=整数位+小数位，D=小数位。 D&lt;=M&lt;=255，0&lt;=D&lt;=30。</p> <p>例如，定义为FLOAT(5,2)的一个列可以显示为-999.99-999.99。如果超过这个范围会报错。</p></li> <li><p>FLOAT和DOUBLE类型在不指定(M,D)时，默认会按照实际的精度（由实际的硬件和操作系统决定）来显示。</p></li> <li><p>说明：浮点类型，也可以加 <code>UNSIGNED</code> ，但是不会改变数据范围，例如：FLOAT(3,2) UNSIGNED仍然只能表示0-9.99的范围。</p></li> <li><p>不管是否显式设置了精度(M,D)，这里MySQL的处理方案如下：</p> <ul><li>如果存储时，整数部分超出了范围，MySQL就会报错，不允许存这样的值</li> <li>如果存储时，小数点部分若超出范围，就分以下情况：
<ul><li>若四舍五入后，整数部分没有超出范围，则只警告，但能成功操作并四舍五入删除多余的小数位后保存。例如在FLOAT(5,2)列内插入999.009，近似结果是999.01。</li> <li>若四舍五入后，整数部分超出范围，则MySQL报错，并拒绝处理。如FLOAT(5,2)列内插入
999.995和-999.995都会报错。</li></ul></li></ul></li> <li><p><strong>从MySQL 8.0.17开始，FLOAT(M,D) 和DOUBLE(M,D)用法在官方文档中已经明确不推荐使用</strong>，将来可能被移除。另外，关于浮点型FLOAT和DOUBLE的UNSIGNED也不推荐使用了，将来也可能被移除。</p></li> <li><p>举例</p></li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_double1<span class="token punctuation">(</span>
    f1 <span class="token keyword">FLOAT</span><span class="token punctuation">,</span>
    f2 <span class="token keyword">FLOAT</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    f3 <span class="token keyword">DOUBLE</span><span class="token punctuation">,</span>
    f4 <span class="token keyword">DOUBLE</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">DESC</span> test_double1<span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_double1
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">123.456</span><span class="token punctuation">,</span><span class="token number">123.456</span><span class="token punctuation">,</span><span class="token number">123.4567</span><span class="token punctuation">,</span><span class="token number">123.45</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#Out of range value for column 'f2' at row 1</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_double1
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">123.456</span><span class="token punctuation">,</span><span class="token number">1234.456</span><span class="token punctuation">,</span><span class="token number">123.4567</span><span class="token punctuation">,</span><span class="token number">123.45</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> test_double1<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h3 id="_3-3-精度误差说明"><a href="#_3-3-精度误差说明" class="header-anchor">#</a> 3.3 精度误差说明</h3> <p>浮点数类型有个缺陷，就是不精准。下面我来重点解释一下为什么 MySQL 的浮点数不够精准。比如，我
们设计一个表，有f1这个字段，插入值分别为0.47,0.44,0.19，我们期待的运行结果是：0.47 + 0.44 + 0.19 =
1.1。而使用sum之后查询：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_double2<span class="token punctuation">(</span>
    f1 <span class="token keyword">DOUBLE</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_double2
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">0.47</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">0.44</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">0.19</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">SUM</span><span class="token punctuation">(</span>f1<span class="token punctuation">)</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> test_double2<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">--------------------+</span>
<span class="token operator">|</span> <span class="token function">SUM</span><span class="token punctuation">(</span>f1<span class="token punctuation">)</span>      		 <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">--------------------+</span>
<span class="token operator">|</span> <span class="token number">1.0999999999999999</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">--------------------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">SUM</span><span class="token punctuation">(</span>f1<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">1.1</span><span class="token punctuation">,</span><span class="token number">1.1</span> <span class="token operator">=</span> <span class="token number">1.1</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> test_double2<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------------+-----------+</span>
<span class="token operator">|</span> <span class="token function">SUM</span><span class="token punctuation">(</span>f1<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">1.1</span> <span class="token operator">|</span> <span class="token number">1.1</span> <span class="token operator">=</span> <span class="token number">1.1</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------+-----------+</span>
<span class="token operator">|</span>       <span class="token number">0</span> 		<span class="token operator">|</span>     <span class="token number">1</span>     <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------+-----------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>查询结果是 1.0999999999999999。看到了吗？虽然误差很小，但确实有误差。 你也可以尝试把数据类型改成 FLOAT，然后运行求和查询，得到的是， 1.0999999940395355。显然，误差更大了。</p> <p>那么，为什么会存在这样的误差呢？问题还是出在 MySQL 对浮点类型数据的存储方式上。</p> <p>MySQL 用 4 个字节存储 FLOAT 类型数据，用 8 个字节来存储 DOUBLE 类型数据。无论哪个，都是采用二进制的方式来进行存储的。比如 9.625，用二进制来表达，就是 1001.101，或者表达成 1.001101×2^3。如果尾数不是 0 或 5（比如 9.624），你就无法用一个二进制数来精确表达。进而，就只好在取值允许的范围内进行四舍五入。</p> <p>在编程中，如果用到浮点数，要特别注意误差问题，<strong>因为浮点数是不准确的，所以我们要避免使用“=”来判断两个数是否相等</strong>。同时，在一些对精确度要求较高的项目中，千万不要使用浮点数，不然会导致结果错误，甚至是造成不可挽回的损失。那么，MySQL 有没有精准的数据类型呢？当然有，这就是定点数类型： <code>DECIMAL</code> 。</p> <h2 id="_4-定点数类型"><a href="#_4-定点数类型" class="header-anchor">#</a> 4. 定点数类型</h2> <h3 id="_4-1-类型介绍"><a href="#_4-1-类型介绍" class="header-anchor">#</a> 4.1 类型介绍</h3> <ul><li>MySQL中的定点数类型只有 DECIMAL 一种类型。</li></ul> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929203.png" alt="image-20220610154657468"></p> <p>​			使用 DECIMAL(M,D) 的方式表示高精度小数。其中，M被称为精度，D被称为标度。0&lt;=M&lt;=65，0&lt;=D&lt;=30，D&lt;M。例如，</p> <p>​			定义DECIMAL（5,2）的类型，表示该列取值范围是-999.99~999.99。</p> <ul><li><strong>DECIMAL(M,D)的最大取值范围与DOUBLE类型一样</strong>，但是有效的数据范围是由M和D决定的。
DECIMAL 的存储空间并不是固定的，由精度值M决定，总共占用的存储空间为M+2个字节。也就是说，在一些对精度要求不高的场景下，比起占用同样字节长度的定点数，浮点数表达的数值范围可以更大一些。</li> <li>定点数在MySQL内部是以 <code>字符串</code> 的形式进行存储，这就决定了它一定是精准的。</li> <li>当DECIMAL类型不指定精度和标度时，其默认为DECIMAL(10,0)。当数据的精度超出了定点数类型的精度范围时，则MySQL同样会进行四舍五入处理。</li> <li><strong>浮点数 vs 定点数</strong> <ul><li>浮点数相对于定点数的优点是在长度一定的情况下，浮点类型取值范围大，但是不精准，适用于需要取值范围大，又可以容忍微小误差的科学计算场景（比如计算化学、分子建模、流体动力学等）</li> <li>定点数类型取值范围相对小，但是精准，没有误差，适合于对精度要求极高的场景 （比如涉及金额计算的场景）</li></ul></li> <li>举例</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_decimal1<span class="token punctuation">(</span>
    f1 <span class="token keyword">DECIMAL</span><span class="token punctuation">,</span>
    f2 <span class="token keyword">DECIMAL</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">DESC</span> test_decimal1<span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_decimal1<span class="token punctuation">(</span>f1<span class="token punctuation">,</span>f2<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">123.123</span><span class="token punctuation">,</span><span class="token number">123.456</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#Out of range value for column 'f2' at row 1</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_decimal1<span class="token punctuation">(</span>f2<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">1234.34</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> test_decimal1<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------+--------+</span>
<span class="token operator">|</span> f1   <span class="token operator">|</span> f2     <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+--------+</span>
<span class="token operator">|</span>  <span class="token number">123</span> <span class="token operator">|</span> <span class="token number">123.46</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+--------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>举例
我们运行下面的语句，把test_double2表中字段“f1”的数据类型修改为 DECIMAL(5,2)：</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> test_double2
<span class="token keyword">MODIFY</span> f1 <span class="token keyword">DECIMAL</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>然后，我们再一次运行求和语句：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">SUM</span><span class="token punctuation">(</span>f1<span class="token punctuation">)</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> test_double2<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------+</span>
<span class="token operator">|</span> <span class="token function">SUM</span><span class="token punctuation">(</span>f1<span class="token punctuation">)</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+</span>
<span class="token operator">|</span>   <span class="token number">1.10</span>  <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">SUM</span><span class="token punctuation">(</span>f1<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">1.1</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> test_double2<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------------+</span>
<span class="token operator">|</span> <span class="token function">SUM</span><span class="token punctuation">(</span>f1<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">1.1</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------+</span>
<span class="token operator">|</span>       <span class="token number">1</span>       <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_4-2-开发中经验"><a href="#_4-2-开发中经验" class="header-anchor">#</a> 4.2 开发中经验</h3> <blockquote><p>“由于 DECIMAL 数据类型的精准性，在我们的项目中，除了极少数（比如商品编号）用到整数类型外，其他的数值都用的是 DECIMAL，原因就是这个项目所处的零售行业，要求精准，一分钱也不能差。 ” ——来自某项目经理</p></blockquote> <h2 id="_5-位类型-bit"><a href="#_5-位类型-bit" class="header-anchor">#</a> 5. 位类型：BIT</h2> <p>BIT类型中存储的是二进制值，类似010110。</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929204.png" alt="image-20220610155907989"></p> <p>BIT类型，如果没有指定(M)，默认是1位。这个1位，表示只能存1位的二进制值。这里(M)是表示二进制的位数，位数最小值为1，最大值为64。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_bit1<span class="token punctuation">(</span>
    f1 <span class="token keyword">BIT</span><span class="token punctuation">,</span>
    f2 <span class="token keyword">BIT</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    f3 <span class="token keyword">BIT</span><span class="token punctuation">(</span><span class="token number">64</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_bit1<span class="token punctuation">(</span>f1<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#Data too long for column 'f1' at row 1</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_bit1<span class="token punctuation">(</span>f1<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_bit1<span class="token punctuation">(</span>f2<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">23</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>注意：在向BIT类型的字段中插入数据时，一定要确保插入的数据在BIT类型支持的范围内。</p> <p>使用SELECT命令查询位字段时，可以用 <code>BIN()</code> 或 <code>HEX()</code> 函数进行读取。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> test_bit1<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------------+------------+------------+</span>
<span class="token operator">|</span> f1         <span class="token operator">|</span> f2         <span class="token operator">|</span> f3         <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------+------------+------------+</span>
<span class="token operator">|</span> <span class="token number">0x01</span>       <span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span> <span class="token number">0x17</span>       <span class="token operator">|</span> <span class="token boolean">NULL</span>       <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------+------------+------------+</span>
<span class="token number">2</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> BIN<span class="token punctuation">(</span>f2<span class="token punctuation">)</span><span class="token punctuation">,</span>HEX<span class="token punctuation">(</span>f2<span class="token punctuation">)</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> test_bit1<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------+---------+</span>
<span class="token operator">|</span> BIN<span class="token punctuation">(</span>f2<span class="token punctuation">)</span> <span class="token operator">|</span> HEX<span class="token punctuation">(</span>f2<span class="token punctuation">)</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+---------+</span>
<span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">10111</span>   <span class="token operator">|</span> <span class="token number">17</span>      <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------+---------+</span>
<span class="token number">2</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> f2 <span class="token operator">+</span> <span class="token number">0</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> test_bit1<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">--------+</span>
<span class="token operator">|</span> f2 <span class="token operator">+</span> <span class="token number">0</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">--------+</span>
<span class="token operator">|</span>  <span class="token boolean">NULL</span>  <span class="token operator">|</span>
<span class="token operator">|</span>   <span class="token number">23</span>   <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">--------+</span>
<span class="token number">2</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>可以看到，使用b+0查询数据时，可以直接查询出存储的十进制数据的值。</p> <h2 id="_6-日期与时间类型"><a href="#_6-日期与时间类型" class="header-anchor">#</a> 6. 日期与时间类型</h2> <p>日期与时间是重要的信息，在我们的系统中，几乎所有的数据表都用得到。原因是客户需要知道数据的时间标签，从而进行数据查询、统计和处理。</p> <p>MySQL有多种表示日期和时间的数据类型，不同的版本可能有所差异，MySQL8.0版本支持的日期和时间类型主要有：YEAR类型、TIME类型、DATE类型、DATETIME类型和TIMESTAMP类型。</p> <ul><li><code>YEAR</code> 类型通常用来表示年</li> <li><code>DATE</code> 类型通常用来表示年、月、日</li> <li><code>TIME</code> 类型通常用来表示时、分、秒</li> <li><code>DATETIME</code> 类型通常用来表示年、月、日、时、分、秒</li> <li><code>TIMESTAMP</code> 类型通常用来表示带时区的年、月、日、时、分、秒</li></ul> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929205.png" alt="image-20220612170319857"></p> <p>可以看到，不同数据类型表示的时间内容不同、取值范围不同，而且占用的字节数也不一样，你要根据实际需要灵活选取。</p> <p>为什么时间类型 TIME 的取值范围不是 -23:59:59～23:59:59 呢？原因是 MySQL 设计的 TIME 类型，不光表示一天之内的时间，而且可以用来表示一个时间间隔，这个时间间隔可以超过 24 小时。</p> <h3 id="_6-1-year类型"><a href="#_6-1-year类型" class="header-anchor">#</a> 6.1 YEAR类型</h3> <p>YEAR类型用来表示年份，在所有的日期时间类型中所占用的存储空间最小，只需要 <code>1个字节</code> 的存储空
间。</p> <p>在MySQL中，YEAR有以下几种存储格式：</p> <ul><li>以4位字符串或数字格式表示YEAR类型，其格式为YYYY，最小值为1901，最大值为2155。</li> <li>以2位字符串格式表示YEAR类型，最小值为00，最大值为99。
<ul><li>当取值为01到69时，表示2001到2069；</li> <li>当取值为70到99时，表示1970到1999；</li> <li>当取值整数的0或00添加的话，那么是0000年；</li> <li>当取值是日期/字符串的'0'添加的话，是2000年。</li></ul></li></ul> <p><strong>从MySQL5.5.27开始，2位格式的YEAR已经不推荐使用</strong>。YEAR默认格式就是“YYYY”，没必要写成YEAR(4)，从MySQL 8.0.19开始，不推荐使用指定显示宽度的YEAR(4)数据类型。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_year<span class="token punctuation">(</span>
    f1 <span class="token keyword">YEAR</span><span class="token punctuation">,</span>
    f2 <span class="token keyword">YEAR</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">DESC</span> test_year<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">-------+---------+------+-----+---------+-------+</span>
<span class="token operator">|</span> Field <span class="token operator">|</span> <span class="token keyword">Type</span>    <span class="token operator">|</span> <span class="token boolean">Null</span> <span class="token operator">|</span> <span class="token keyword">Key</span> <span class="token operator">|</span> <span class="token keyword">Default</span> <span class="token operator">|</span> Extra <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">-------+---------+------+-----+---------+-------+</span>
<span class="token operator">|</span> f1    <span class="token operator">|</span> <span class="token keyword">year</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> <span class="token boolean">NULL</span>    <span class="token operator">|</span>       <span class="token operator">|</span>
<span class="token operator">|</span> f2    <span class="token operator">|</span> <span class="token keyword">year</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> <span class="token boolean">NULL</span>  	 <span class="token operator">|</span>       <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">-------+---------+------+-----+---------+-------+</span>
<span class="token number">2</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_year
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'2020'</span><span class="token punctuation">,</span><span class="token string">'2021'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> test_year<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------+------+</span>
<span class="token operator">|</span> f1   <span class="token operator">|</span> f2   <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------+</span>
<span class="token operator">|</span> <span class="token number">2020</span> <span class="token operator">|</span> <span class="token number">2021</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------+</span>
<span class="token number">1</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_year
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'45'</span><span class="token punctuation">,</span><span class="token string">'71'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_year
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token string">'0'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> test_year<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------+------+</span>
<span class="token operator">|</span> f1   <span class="token operator">|</span> f2   <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------+</span>
<span class="token operator">|</span> <span class="token number">2020</span> <span class="token operator">|</span> <span class="token number">2021</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">2045</span> <span class="token operator">|</span> <span class="token number">1971</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">0000</span> <span class="token operator">|</span> <span class="token number">2000</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------+</span>
<span class="token number">3</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_6-2-date类型"><a href="#_6-2-date类型" class="header-anchor">#</a> 6.2 DATE类型</h3> <p>DATE类型表示日期，没有时间部分，格式为 <code>YYYY-MM-DD</code> ，其中，YYYY表示年份，MM表示月份，DD表示日期。需要 <code>3个字节</code> 的存储空间。在向DATE类型的字段插入数据时，同样需要满足一定的格式条件。</p> <ul><li>以 <code>YYYY-MM-DD</code> 格式或者 <code>YYYYMMDD</code> 格式表示的字符串日期，其最小取值为1000-01-01，最大取值为9999-12-03。YYYYMMDD格式会被转化为YYYY-MM-DD格式。</li> <li>以 <code>YY-MM-DD</code> 格式或者 <code>YYMMDD</code> 格式表示的字符串日期，此格式中，年份为两位数值或字符串满足YEAR类型的格式条件为：当年份取值为00到69时，会被转化为2000到2069；当年份取值为70到99时，会被转化为1970到1999。</li> <li>使用 <code>CURRENT_DATE()</code> 或者 <code>NOW()</code> 函数，会插入当前系统的日期。</li></ul> <p><strong>举例</strong>：</p> <p>创建数据表，表中只包含一个DATE类型的字段f1。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_date1<span class="token punctuation">(</span>
    f1 <span class="token keyword">DATE</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
Query OK<span class="token punctuation">,</span> <span class="token number">0</span> <span class="token keyword">rows</span> affected <span class="token punctuation">(</span><span class="token number">0.13</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>插入数据：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_date1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'2020-10-01'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'20201001'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">20201001</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_date1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'00-01-01'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'000101'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'69-10-01'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'691001'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'70-01-01'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'700101'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">(</span><span class="token string">'99-01-01'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'990101'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_date1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">000301</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">690301</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">700301</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">990301</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_date1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token keyword">CURRENT_DATE</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> <span class="token operator">*</span>
<span class="token keyword">FROM</span> test_date1<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_6-3-time类型"><a href="#_6-3-time类型" class="header-anchor">#</a> 6.3 TIME类型</h3> <p>TIME类型用来表示时间，不包含日期部分。在MySQL中，需要 <code>3个字节</code> 的存储空间来存储TIME类型的数据，可以使用“HH:MM:SS”格式来表示TIME类型，其中，HH表示小时，MM表示分钟，SS表示秒。</p> <p>在MySQL中，向TIME类型的字段插入数据时，也可以使用几种不同的格式。</p> <p>（1）可以使用带有冒号的字符串，比如' <code>D HH:MM:SS</code>' 、' <code>HH:MM:SS</code> '、' <code>HH:MM</code> '、' <code>D HH:MM</code> '、' <code>D HH</code> '或' <code>SS</code> '格式，都能被正确地插入TIME类型的字段中。其中D表示天，其最小值为0，最大值为34。如果使用带有D格式的字符串插入TIME类型的字段时，D会被转化为小时，计算格式为D*24+HH。当使用带有冒号并且不带D的字符串表示时间时，表示当天的时间，比如12:10表示12:10:00，而不是00:12:10。</p> <p>（2）可以使用不带有冒号的字符串或者数字，格式为' HHMMSS '或者 HHMMSS 。如果插入一个不合法的字符串或者数字，MySQL在存储数据时，会将其自动转化为00:00:00进行存储。比如1210，MySQL会将最右边的两位解析成秒，表示00:12:10，而不是12:10:00。</p> <p>（3）使用 <code>CURRENT_TIME()</code> 或者 <code>NOW()</code> ，会插入当前系统的时间。</p> <p><strong>举例</strong>：</p> <p>创建数据表，表中包含一个TIME类型的字段f1。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_time1<span class="token punctuation">(</span>
    f1 <span class="token keyword">TIME</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
Query OK<span class="token punctuation">,</span> <span class="token number">0</span> <span class="token keyword">rows</span> affected <span class="token punctuation">(</span><span class="token number">0.02</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_time1
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'2 12:30:29'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'12:35:29'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'12:40'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'2 12:40'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">'1 05'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'45'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_time1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'123520'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">124011</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">1210</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_time1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">CURRENT_TIME</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> test_time1<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="_6-4-datetime类型"><a href="#_6-4-datetime类型" class="header-anchor">#</a> 6.4 DATETIME类型</h3> <p>DATETIME类型在所有的日期时间类型中占用的存储空间最大，总共需要 <code>8</code> 个字节的存储空间。在格式上为DATE类型和TIME类型的组合，可以表示为 <code>YYYY-MM-DD HH:MM:SS</code> ，其中YYYY表示年份，MM表示月份，DD表示日期，HH表示小时，MM表示分钟，SS表示秒。</p> <p>在向DATETIME类型的字段插入数据时，同样需要满足一定的格式条件。</p> <ul><li>以 <code>YYYY-MM-DD HH:MM:SS</code> 格式或者 <code>YYYYMMDDHHMMSS</code> 格式的字符串插入DATETIME类型的字段时，最小值为1000-01-01 00:00:00，最大值为9999-12-03 23:59:59。
<ul><li>以YYYYMMDDHHMMSS格式的数字插入DATETIME类型的字段时，会被转化为YYYY-MM-DD HH:MM:SS格式。</li> <li>以 <code>YY-MM-DD HH:MM:SS</code> 格式或者 <code>YYMMDDHHMMSS</code> 格式的字符串插入DATETIME类型的字段时，两位数的年份规则符合YEAR类型的规则，00到69表示2000到2069；70到99表示1970到1999。</li></ul></li> <li>使用函数 CURRENT_TIMESTAMP() 和 NOW() ，可以向DATETIME类型的字段插入系统的当前日期和时间。</li></ul> <p><strong>举例</strong>：</p> <p>创建数据表，表中包含一个DATETIME类型的字段dt。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_datetime1<span class="token punctuation">(</span>
    dt <span class="token keyword">DATETIME</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
Query OK<span class="token punctuation">,</span> <span class="token number">0</span> <span class="token keyword">rows</span> affected <span class="token punctuation">(</span><span class="token number">0.02</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>插入数据：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_datetime1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'2021-01-01 06:50:30'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'20210101065030'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_datetime1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'99-01-01 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'990101000000'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'20-01-01 00:00:00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">'200101000000'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_datetime1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">20200101000000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">200101000000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">19990101000000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">990101000000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_datetime1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token keyword">CURRENT_TIMESTAMP</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="_6-5-timestamp类型"><a href="#_6-5-timestamp类型" class="header-anchor">#</a> 6.5 TIMESTAMP类型</h3> <p>TIMESTAMP类型也可以表示日期时间，其显示格式与DATETIME类型相同，都是 <code>YYYY-MM-DD HH:MM:SS</code> ，需要4个字节的存储空间。但是TIMESTAMP存储的时间范围比DATETIME要小很多，只能存储“1970-01-01 00:00:01 UTC”到“2038-01-19 03:14:07 UTC”之间的时间。其中，UTC表示世界统一时间，也叫作世界标准时间。</p> <ul><li><strong>存储数据的时候需要对当前时间所在的时区进行转换，查询数据的时候再将时间转换回当前的时</strong> <strong>区。因此，使用TIMESTAMP存储的同一个时间值，在不同的时区查询时会显示不同的时间</strong>。</li></ul> <p>向TIMESTAMP类型的字段插入数据时，当插入的数据格式满足YY-MM-DD HH:MM:SS和YYMMDDHHMMSS时，两位数值的年份同样符合YEAR类型的规则条件，只不过表示的时间范围要小很多。</p> <p>如果向TIMESTAMP类型的字段插入的时间超出了TIMESTAMP类型的范围，则MySQL会抛出错误信息。</p> <p><strong>举例</strong>：</p> <p>创建数据表，表中包含一个TIMESTAMP类型的字段ts。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_timestamp1<span class="token punctuation">(</span>
    ts <span class="token keyword">TIMESTAMP</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>插入数据：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_timestamp1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'1999-01-01 03:04:50'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'19990101030405'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'99-01-01 03:04:05'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">'990101030405'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_timestamp1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'2020@01@01@00@00@00'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'20@01@01@00@00@00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_timestamp1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token keyword">CURRENT_TIMESTAMP</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#Incorrect datetime value</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_timestamp1
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'2038-01-20 03:14:07'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><strong>TIMESTAMP和DATETIME的区别</strong>：</p> <ul><li>TIMESTAMP存储空间比较小，表示的日期时间范围也比较小</li> <li>底层存储方式不同，TIMESTAMP底层存储的是毫秒值，距离1970-1-1 0:0:0 0毫秒的毫秒值。</li> <li>两个日期比较大小或日期计算时，TIMESTAMP更方便、更快。</li> <li>TIMESTAMP和时区有关。TIMESTAMP会根据用户的时区不同，显示不同的结果。而DATETIME则只能反映出插入时当地的时区，其他时区的人查看数据必然会有误差的。</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> temp_time<span class="token punctuation">(</span>
    d1 <span class="token keyword">DATETIME</span><span class="token punctuation">,</span>
    d2 <span class="token keyword">TIMESTAMP</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> temp_time <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'2021-9-2 14:45:52'</span><span class="token punctuation">,</span><span class="token string">'2021-9-2 14:45:52'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> temp_time <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">NOW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> temp_time<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------------------+---------------------+</span>
<span class="token operator">|</span> d1         		  <span class="token operator">|</span> d2                  <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------------+---------------------+</span>
<span class="token operator">|</span> <span class="token number">2021</span><span class="token operator">-</span><span class="token number">09</span><span class="token operator">-</span><span class="token number">02</span> <span class="token number">14</span>:<span class="token number">45</span>:<span class="token number">52</span> <span class="token operator">|</span> <span class="token number">2021</span><span class="token operator">-</span><span class="token number">09</span><span class="token operator">-</span><span class="token number">02</span> <span class="token number">14</span>:<span class="token number">45</span>:<span class="token number">52</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">2021</span><span class="token operator">-</span><span class="token number">11</span><span class="token operator">-</span><span class="token number">03</span> <span class="token number">17</span>:<span class="token number">38</span>:<span class="token number">17</span> <span class="token operator">|</span> <span class="token number">2021</span><span class="token operator">-</span><span class="token number">11</span><span class="token operator">-</span><span class="token number">03</span> <span class="token number">17</span>:<span class="token number">38</span>:<span class="token number">17</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------------+---------------------+</span>
<span class="token number">2</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token comment">#修改当前的时区</span>
<span class="token keyword">SET</span> time_zone <span class="token operator">=</span> <span class="token string">'+9:00'</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> temp_time<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">---------------------+---------------------+</span>
<span class="token operator">|</span> d1                  <span class="token operator">|</span> d2                  <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------------+---------------------+</span>
<span class="token operator">|</span> <span class="token number">2021</span><span class="token operator">-</span><span class="token number">09</span><span class="token operator">-</span><span class="token number">02</span> <span class="token number">14</span>:<span class="token number">45</span>:<span class="token number">52</span> <span class="token operator">|</span> <span class="token number">2021</span><span class="token operator">-</span><span class="token number">09</span><span class="token operator">-</span><span class="token number">02</span> <span class="token number">15</span>:<span class="token number">45</span>:<span class="token number">52</span> <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token number">2021</span><span class="token operator">-</span><span class="token number">11</span><span class="token operator">-</span><span class="token number">03</span> <span class="token number">17</span>:<span class="token number">38</span>:<span class="token number">17</span> <span class="token operator">|</span> <span class="token number">2021</span><span class="token operator">-</span><span class="token number">11</span><span class="token operator">-</span><span class="token number">03</span> <span class="token number">18</span>:<span class="token number">38</span>:<span class="token number">17</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">---------------------+---------------------+</span>
<span class="token number">2</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_6-6-开发中经验"><a href="#_6-6-开发中经验" class="header-anchor">#</a> 6.6 开发中经验</h3> <p>用得最多的日期时间类型，就是  <code>DATETIME</code> 。虽然 MySQL 也支持 YEAR（年）、 TIME（时间）、DATE（日期），以及 TIMESTAMP 类型，但是在实际项目中，尽量用 DATETIME 类型。因为这个数据类型包括了完整的日期和时间信息，取值范围也最大，使用起来比较方便。毕竟，如果日期时间信息分散在好几个字段，很不容易记，而且查询的时候，SQL 语句也会更加复杂。</p> <p>此外，一般存注册时间、商品发布时间等，不建议使用DATETIME存储，而是使用 <code>时间戳</code> ，因为DATETIME虽然直观，但不便于计算。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> UNIX_TIMESTAMP<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------------------+</span>
<span class="token operator">|</span> UNIX_TIMESTAMP<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------------+</span>
<span class="token operator">|</span>    <span class="token number">1635932762</span>    <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="_7-文本字符串类型"><a href="#_7-文本字符串类型" class="header-anchor">#</a> 7. 文本字符串类型</h2> <p>在实际的项目中，我们还经常遇到一种数据，就是字符串数据。
MySQL中，文本字符串总体上分为 <code>CHAR</code> 、 <code>VARCHAR</code> 、 <code>TINYTEXT</code> 、 <code>TEXT</code> 、 <code>MEDIUMTEXT</code> 、<code>LONGTEXT</code> 、 <code>ENUM</code> 、 <code>SET</code> 等类型。</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929206.png" alt="image-20220612213114464"></p> <h3 id="_7-1-char与varchar类型"><a href="#_7-1-char与varchar类型" class="header-anchor">#</a> 7.1 CHAR与VARCHAR类型</h3> <p>CHAR和VARCHAR类型都可以存储比较短的字符串。</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929207.png" alt="image-20220612213139955"></p> <p><strong>CHAR类型</strong>：</p> <ul><li>CHAR(M) 类型一般需要预先定义字符串长度。如果不指定(M)，则表示长度默认是1个字符。</li> <li>如果保存时，数据的实际长度比CHAR类型声明的长度小，则会在 右侧填充 空格以达到指定的长度。当MySQL检索CHAR类型的数据时，CHAR类型的字段会去除尾部的空格。</li> <li>定义CHAR类型字段时，声明的字段长度即为CHAR类型字段所占的存储空间的字节数。</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_char1<span class="token punctuation">(</span>
    c1 <span class="token keyword">CHAR</span><span class="token punctuation">,</span>
    c2 <span class="token keyword">CHAR</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">DESC</span> test_char1<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_char1
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'Tom'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> c1<span class="token punctuation">,</span>CONCAT<span class="token punctuation">(</span>c2<span class="token punctuation">,</span><span class="token string">'***'</span><span class="token punctuation">)</span> <span class="token keyword">FROM</span> test_char1<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_char1<span class="token punctuation">(</span>c2<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'a '</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> CHAR_LENGTH<span class="token punctuation">(</span>c2<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> test_char1<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>VARCHAR类型</strong>：</p> <ul><li>VARCHAR(M) 定义时， <code>必须指定</code> 长度M，否则报错。</li> <li>MySQL4.0版本以下，varchar(20)：指的是20字节，如果存放UTF8汉字时，只能存6个（每个汉字3字节） ；MySQL5.0版本以上，varchar(20)：指的是20字符。</li> <li>检索VARCHAR类型的字段数据时，会保留数据尾部的空格。VARCHAR类型的字段所占用的存储空间为字符串实际长度加1个字节。</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_varchar1<span class="token punctuation">(</span>
    NAME <span class="token keyword">VARCHAR</span>  <span class="token comment">#错误</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token comment">#Column length too big for column 'NAME' (max = 21845);</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_varchar2<span class="token punctuation">(</span>
    NAME <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">65535</span><span class="token punctuation">)</span>  <span class="token comment">#错误</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_varchar3<span class="token punctuation">(</span>
    NAME <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_varchar3
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'尚硅谷'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">'尚硅谷教育'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#Data too long for column 'NAME' at row 1</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_varchar3
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'尚硅谷IT教育'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><strong>哪些情况使用 CHAR 或 VARCHAR 更好</strong></p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929208.png" alt="image-20220612213248448"></p> <ul><li><p>情况1：存储很短的信息。比如门牌号码101，201……这样很短的信息应该用char，因为varchar还要占个byte用于存储信息长度，本来打算节约存储的，结果得不偿失。</p></li> <li><p>情况2：固定长度的。比如使用uuid作为主键，那用char应该更合适。因为他固定长度，varchar动态根据长度的特性就消失了，而且还要占个长度信息。</p></li> <li><p>情况3：十分频繁改变的column。因为varchar每次存储都要有额外的计算，得到长度等工作，如果一个非常频繁改变的，那就要有很多的精力用于计算，而这些对于char来说是不需要的。</p></li> <li><p>情况4：具体存储引擎中的情况：</p> <ul><li><code>MyISAM</code> 数据存储引擎和数据列：MyISAM数据表，最好使用固定长度(CHAR)的数据列代替可变长度(VARCHAR)的数据列。这样使得整个表静态化，从而使 <code>数据检索更快</code> ，用空间换时间。</li> <li><code>MEMORY</code> 存储引擎和数据列：MEMORY数据表目前都使用固定长度的数据行存储，因此无论使用CHAR或VARCHAR列都没有关系，两者都是作为CHAR类型处理的。</li> <li><code>InnoDB</code> 存储引擎，建议使用VARCHAR类型。因为对于InnoDB数据表，内部的行存储格式并没有区分固定长度和可变长度列（所有数据行都使用指向数据列值的头指针），而且<strong>主要影响性能的因素是数据行使用的存储总量</strong>，由于char平均占用的空间多于varchar，所以除了简短并且固定长度的，其他考虑varchar。这样节省空间，对磁盘I/O和数据存储总量比较好。</li></ul></li></ul> <h3 id="_7-2-text类型"><a href="#_7-2-text类型" class="header-anchor">#</a> 7.2 TEXT类型</h3> <p>在MySQL中，TEXT用来保存文本类型的字符串，总共包含4种类型，分别为TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT 类型。</p> <p>在向TEXT类型的字段保存和查询数据时，系统自动按照实际长度存储，不需要预先定义长度。这一点和VARCHAR类型相同。</p> <p>每种TEXT类型保存的数据长度和所占用的存储空间不同，如下：</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929209.png" alt="image-20220612213327497"></p> <p><strong>由于实际存储的长度不确定，MySQL 不允许 TEXT 类型的字段做主键</strong>。遇到这种情况，你只能采用CHAR(M)，或者 VARCHAR(M)。</p> <p>举例：</p> <p>创建数据表：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_text<span class="token punctuation">(</span>
    tx <span class="token keyword">TEXT</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_text
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'atguigu  '</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> CHAR_LENGTH<span class="token punctuation">(</span>tx<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> test_text<span class="token punctuation">;</span> <span class="token comment">#10</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>说明在保存和查询数据时，并没有删除TEXT类型的数据尾部的空格。</p> <p><strong>开发中经验</strong>：
TEXT文本类型，可以存比较大的文本段，搜索速度稍慢，因此如果不是特别大的内容，建议使用CHAR，VARCHAR来代替。还有TEXT类型不用加默认值，加了也没用。而且text和blob类型的数据删除后容易导致“空洞”，使得文件碎片比较多，所以频繁使用的表不建议包含TEXT类型字段，建议单独分出去，单独用一个表。</p> <h2 id="_8-enum类型"><a href="#_8-enum类型" class="header-anchor">#</a> 8. ENUM类型</h2> <p>ENUM类型也叫作枚举类型，ENUM类型的取值范围需要在定义字段时进行指定。设置字段值时，ENUM类型只允许从成员中选取单个值，不能一次选取多个值。</p> <p>其所需要的存储空间由定义ENUM类型时指定的成员个数决定。</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929210.png" alt="image-20220612213424640"></p> <ul><li>当ENUM类型包含1～255个成员时，需要1个字节的存储空间</li> <li>当ENUM类型包含256～65535个成员时，需要2个字节的存储空间。</li> <li>ENUM类型的成员个数的上限为65535个。</li></ul> <p>举例：</p> <p>创建表如下：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_enum<span class="token punctuation">(</span>
    season <span class="token keyword">ENUM</span><span class="token punctuation">(</span><span class="token string">'春'</span><span class="token punctuation">,</span><span class="token string">'夏'</span><span class="token punctuation">,</span><span class="token string">'秋'</span><span class="token punctuation">,</span><span class="token string">'冬'</span><span class="token punctuation">,</span><span class="token string">'unknow'</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>添加数据：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_enum
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'春'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">'秋'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment"># 忽略大小写</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_enum
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'UNKNOW'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment"># 允许按照角标的方式获取指定索引位置的枚举值</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_enum
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'1'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment"># Data truncated for column 'season' at row 1</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_enum
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'ab'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment"># 当ENUM类型的字段没有声明为NOT NULL时，插入NULL也是有效的</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_enum
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token boolean">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h2 id="_9-set类型"><a href="#_9-set类型" class="header-anchor">#</a> 9. SET类型</h2> <p>SET表示一个字符串对象，可以包含0个或多个成员，但成员个数的上限为 <code>64</code> 。设置字段值时，可以取取值范围内的 0 个或多个值。</p> <p>当SET类型包含的成员个数不同时，其所占用的存储空间也是不同的，具体如下：</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929211.png" alt="image-20220612213517061"></p> <p>SET类型在存储数据时成员个数越多，其占用的存储空间越大。注意：SET类型在选取成员时，可以一次选择多个成员，这一点与ENUM类型不同。</p> <p>举例：</p> <p>创建表：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_set<span class="token punctuation">(</span>
    s <span class="token keyword">SET</span> <span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>向表中插入数据：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_set <span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">'A,B'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#插入重复的SET类型成员时，MySQL会自动删除重复的成员</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_set <span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'A,B,C,A'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#向SET类型的字段插入SET成员中不存在的值时，MySQL会抛出错误。</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_set <span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'A,B,C,D'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> <span class="token operator">*</span>
<span class="token keyword">FROM</span> test_set<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>举例：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> temp_mul<span class="token punctuation">(</span>
    gender <span class="token keyword">ENUM</span><span class="token punctuation">(</span><span class="token string">'男'</span><span class="token punctuation">,</span><span class="token string">'女'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    hobby <span class="token keyword">SET</span><span class="token punctuation">(</span><span class="token string">'吃饭'</span><span class="token punctuation">,</span><span class="token string">'睡觉'</span><span class="token punctuation">,</span><span class="token string">'打豆豆'</span><span class="token punctuation">,</span><span class="token string">'写代码'</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> temp_mul <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'男'</span><span class="token punctuation">,</span><span class="token string">'睡觉,打豆豆'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">#成功</span>

<span class="token comment"># Data truncated for column 'gender' at row 1</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> temp_mul <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'男,女'</span><span class="token punctuation">,</span><span class="token string">'睡觉,写代码'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">#失败</span>

<span class="token comment"># Data truncated for column 'gender' at row 1</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> temp_mul <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'妖'</span><span class="token punctuation">,</span><span class="token string">'睡觉,写代码'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">#失败</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> temp_mul <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'男'</span><span class="token punctuation">,</span><span class="token string">'睡觉,写代码,吃饭'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">#成功</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="_10-二进制字符串类型"><a href="#_10-二进制字符串类型" class="header-anchor">#</a> 10. 二进制字符串类型</h2> <p>MySQL中的二进制字符串类型主要存储一些二进制数据，比如可以存储图片、音频和视频等二进制数据。</p> <p>MySQL中支持的二进制字符串类型主要包括BINARY、VARBINARY、TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB类型。</p> <h3 id="binary与varbinary类型"><a href="#binary与varbinary类型" class="header-anchor">#</a> BINARY与VARBINARY类型</h3> <p>BINARY和VARBINARY类似于CHAR和VARCHAR，只是它们存储的是二进制字符串。</p> <p>BINARY (M)为固定长度的二进制字符串，M表示最多能存储的字节数，取值范围是0~255个字符。如果未指定(M)，表示只能存储 1个字节 。例如BINARY (8)，表示最多能存储8个字节，如果字段值不足(M)个字节，将在右边填充'\0'以补齐指定长度。</p> <p>VARBINARY (M)为可变长度的二进制字符串，M表示最多能存储的字节数，总字节数不能超过行的字节长度限制65535，另外还要考虑额外字节开销，VARBINARY类型的数据除了存储数据本身外，还需要1或2个字节来存储数据的字节数。VARBINARY类型 <code>必须指定(M)</code> ，否则报错。</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929212.png" alt="image-20220612213628805"></p> <p>举例：</p> <p>创建表：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_binary1<span class="token punctuation">(</span>
    f1 <span class="token keyword">BINARY</span><span class="token punctuation">,</span>
    f2 <span class="token keyword">BINARY</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token comment"># f3 VARBINARY,</span>
    f4 <span class="token keyword">VARBINARY</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>添加数据：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_binary1<span class="token punctuation">(</span>f1<span class="token punctuation">,</span>f2<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'a'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_binary1<span class="token punctuation">(</span>f1<span class="token punctuation">,</span>f2<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'尚'</span><span class="token punctuation">,</span><span class="token string">'尚'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">#失败</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_binary1<span class="token punctuation">(</span>f2<span class="token punctuation">,</span>f4<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token string">'ab'</span><span class="token punctuation">,</span><span class="token string">'ab'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> LENGTH<span class="token punctuation">(</span>f2<span class="token punctuation">)</span><span class="token punctuation">,</span>LENGTH<span class="token punctuation">(</span>f4<span class="token punctuation">)</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> test_binary1<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------------+------------+</span>
<span class="token operator">|</span> LENGTH<span class="token punctuation">(</span>f2<span class="token punctuation">)</span> <span class="token operator">|</span> LENGTH<span class="token punctuation">(</span>f4<span class="token punctuation">)</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------+------------+</span>
<span class="token operator">|</span>      <span class="token number">3</span>     <span class="token operator">|</span>    <span class="token boolean">NULL</span>    <span class="token operator">|</span>
<span class="token operator">|</span>      <span class="token number">3</span>     <span class="token operator">|</span>      <span class="token number">2</span>     <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------+------------+</span>
<span class="token number">2</span> <span class="token keyword">rows</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="blob类型"><a href="#blob类型" class="header-anchor">#</a> BLOB类型</h3> <p>BLOB是一个 <code>二进制大对象</code> ，可以容纳可变数量的数据。
MySQL中的BLOB类型包括TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB 4种类型，它们可容纳值的最大长度不同。可以存储一个二进制的大对象，比如 <code>图片</code> 、 <code>音频</code> 和 <code>视频</code> 等。</p> <p>需要注意的是，在实际工作中，往往不会在MySQL数据库中使用BLOB类型存储大对象数据，通常会将图片、音频和视频文件存储到 <code>服务器的磁盘上</code> ，并将图片、音频和视频的访问路径存储到MySQL中。</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929213.png" alt="image-20220612213729245"></p> <p>举例：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>CREATE TABLE test_blob1(
id INT,
img MEDIUMBLOB
);
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>TEXT和BLOB的使用注意事项</strong>：</p> <p>在使用text和blob字段类型时要注意以下几点，以便更好的发挥数据库的性能。</p> <p>① BLOB和TEXT值也会引起自己的一些问题，特别是执行了大量的删除或更新操作的时候。删除这种值会在数据表中留下很大的&quot; <code>空洞</code>&quot;，以后填入这些&quot;空洞&quot;的记录可能长度不同。为了提高性能，建议定期使用 OPTIMIZE TABLE 功能对这类表进行 <code>碎片整理</code> 。</p> <p>② 如果需要对大文本字段进行模糊查询，MySQL 提供了 <code>前缀索引</code> 。但是仍然要在不必要的时候避免检索大型的BLOB或TEXT值。例如，SELECT * 查询就不是很好的想法，除非你能够确定作为约束条件的WHERE子句只会找到所需要的数据行。否则，你可能毫无目的地在网络上传输大量的值。</p> <p>③ 把BLOB或TEXT列 <code>分离到单独的表</code> 中。在某些环境中，如果把这些数据列移动到第二张数据表中，可以让你把原数据表中的数据列转换为固定长度的数据行格式，那么它就是有意义的。这会 <code>减少主表中的碎片</code> ，使你得到固定长度数据行的性能优势。它还使你在主数据表上运行 SELECT * 查询的时候不会通过网络传输大量的BLOB或TEXT值。</p> <h2 id="_11-json-类型"><a href="#_11-json-类型" class="header-anchor">#</a> 11. JSON 类型</h2> <p>JSON（JavaScript Object Notation）是一种轻量级的 <code>数据交换格式</code> 。简洁和清晰的层次结构使得 JSON 成
为理想的数据交换语言。它易于人阅读和编写，同时也易于机器解析和生成，并有效地提升网络传输效
率。<strong>JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串，然后就可以在网络或者程序之间轻</strong> <strong>松地传递这个字符串，并在需要的时候将它还原为各编程语言所支持的数据格式。</strong></p> <p>在MySQL 5.7中，就已经支持JSON数据类型。在MySQL 8.x版本中，JSON类型提供了可以进行自动验证的JSON文档和优化的存储结构，使得在MySQL中存储和读取JSON类型的数据更加方便和高效。 创建数据表，表中包含一个JSON类型的字段 js。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> test_json<span class="token punctuation">(</span>
    js json
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>向表中插入JSON数据。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> test_json <span class="token punctuation">(</span>js<span class="token punctuation">)</span>
<span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token string">'{&quot;name&quot;:&quot;songhk&quot;, &quot;age&quot;:18, &quot;address&quot;:{&quot;province&quot;:&quot;beijing&quot;,&quot;city&quot;:&quot;beijing&quot;}}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>查询t19表中的数据。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span>
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> test_json<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929214.png" alt="image-20220612213834616"></p> <p>当需要检索JSON类型的字段中数据的某个具体值时，可以使用“-&gt;”和“-&gt;&gt;”符号。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">SELECT</span> js <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token string">'$.name'</span> <span class="token keyword">AS</span> NAME<span class="token punctuation">,</span>js <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token string">'$.age'</span> <span class="token keyword">AS</span> age <span class="token punctuation">,</span>js <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token string">'$.address.province'</span>
<span class="token keyword">AS</span> province<span class="token punctuation">,</span> js <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token string">'$.address.city'</span> <span class="token keyword">AS</span> city
 <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token keyword">FROM</span> test_json<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">----------+------+-----------+-----------+</span>
<span class="token operator">|</span> NAME     <span class="token operator">|</span> age  <span class="token operator">|</span> province  <span class="token operator">|</span> city      <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----------+------+-----------+-----------+</span>
<span class="token operator">|</span> <span class="token string">&quot;songhk&quot;</span> <span class="token operator">|</span> <span class="token number">18</span>   <span class="token operator">|</span> <span class="token string">&quot;beijing&quot;</span> <span class="token operator">|</span> <span class="token string">&quot;beijing&quot;</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">----------+------+-----------+-----------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>通过“-&gt;”和“-&gt;&gt;”符号，从JSON字段中正确查询出了指定的JSON数据的值。</p> <h2 id="_12-空间类型"><a href="#_12-空间类型" class="header-anchor">#</a> 12. 空间类型</h2> <p>MySQL 空间类型扩展支持地理特征的生成、存储和分析。这里的地理特征表示世界上具有位置的任何东西，可以是一个实体，例如一座山；可以是空间，例如一座办公楼；也可以是一个可定义的位置，例如一个十字路口等等。MySQL中使用 <code>Geometry（几何）</code>来表示所有地理特征。Geometry指一个点或点的集合，代表世界上任何具有位置的事物。</p> <p>MySQL的空间数据类型（Spatial Data Type）对应于OpenGIS类，包括单值类型：GEOMETRY、POINT、LINESTRING、POLYGON以及集合类型：MULTIPOINT、MULTILINESTRING、MULTIPOLYGON、GEOMETRYCOLLECTION 。</p> <ul><li><p>Geometry是所有空间集合类型的基类，其他类型如POINT、LINESTRING、POLYGON都是Geometry的子类。</p> <ul><li><p>Point，顾名思义就是点，有一个坐标值。例如POINT(121.213342 31.234532)，POINT(30 10)，坐标值支持DECIMAL类型，经度（longitude）在前，维度（latitude）在后，用空格分隔。</p></li> <li><p>LineString，线，由一系列点连接而成。如果线从头至尾没有交叉，那就是简单的（simple）；如果起点和终点重叠，那就是封闭的（closed）。例如LINESTRING(30 10,10 30,40 40)，点与点之间用逗号分隔，一个点中的经纬度用空格分隔，与POINT格式一致。</p></li> <li><p>Polygon，多边形。可以是一个实心平面形，即没有内部边界，也可以有空洞，类似纽扣。最简单的就是只有一个外边界的情况，例如POLYGON((0 0,10 0,10 10, 0 10))。</p></li></ul></li></ul> <p>下面展示几种常见的几何图形元素：</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929215.png" alt="image-20220612213927876"></p> <ul><li>MultiPoint、MultiLineString、MultiPolygon、GeometryCollection 这4种类型都是集合类，是多个Point、LineString或Polygon组合而成。</li></ul> <p>下面展示的是多个同类或异类几何图形元素的组合：</p> <p><img src="https://lcfimgsbed.oss-cn-chengdu.aliyuncs.com/img/202207251929216.png" alt="image-20220612213938899"></p> <h2 id="_13-小结及选择建议"><a href="#_13-小结及选择建议" class="header-anchor">#</a> 13. 小结及选择建议</h2> <p>在定义数据类型时，如果确定是 <code>整数</code> ，就用 <code>INT</code> ； 如果是 <code>小数</code> ，一定用定点数类型 <code>DECIMAL(M,D)</code> ； 如果是日期与时间，就用  <code>DATETIME</code> 。</p> <p>这样做的好处是，首先确保你的系统不会因为数据类型定义出错。不过，凡事都是有两面的，可靠性好，并不意味着高效。比如，TEXT 虽然使用方便，但是效率不如 CHAR(M) 和 VARCHAR(M)。</p> <p>关于字符串的选择，建议参考如下阿里巴巴的《Java开发手册》规范：</p> <p><strong>阿里巴巴《Java开发手册》之MySQL数据库</strong>：</p> <ul><li>任何字段如果为非负数，必须是 UNSIGNED</li> <li>【 <code>强制</code> 】小数类型为 DECIMAL，禁止使用 FLOAT 和 DOUBLE。
<ul><li>说明：在存储的时候，FLOAT 和 DOUBLE 都存在精度损失的问题，很可能在比较值的时候，得到不正确的结果。如果存储的数据范围超过 DECIMAL 的范围，建议将数据拆成整数和小数并分开存储。</li></ul></li> <li>【 <code>强制</code> 】如果存储的字符串长度几乎相等，使用 CHAR 定长字符串类型。</li> <li>【 <code>强制</code> 】VARCHAR 是可变长字符串，不预先分配存储空间，长度不要超过 5000。如果存储长度大于此值，定义字段类型为 TEXT，独立出来一张表，用主键来对应，避免影响其它字段索引效率。</li></ul></div></div> <!----> <div class="page-edit"><div class="edit-link"><a href="https://github.com/lcfqzd/vuepress-theme-vdoing/edit/master/docs/04.数据库/01.MySQL笔记/12.MySQL数据类型精讲.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/noteslibrary/tags/?tag=MySQL" title="标签">#MySQL</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/noteslibrary/pages/a20c5b/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">数据处理之增删改</div></a> <a href="/noteslibrary/pages/98e681/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">约束</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/noteslibrary/pages/a20c5b/" class="prev">数据处理之增删改</a></span> <span class="next"><a href="/noteslibrary/pages/98e681/">约束</a>→
      </span></p></div></div></div> <!----></main></div> <div class="footer"><!----> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2018-2023
    <span>LCFQZD | MIT License</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"></div></div>
    <script src="/noteslibrary/assets/js/app.dc0c3d24.js" defer></script><script src="/noteslibrary/assets/js/2.a664539c.js" defer></script><script src="/noteslibrary/assets/js/99.e024f354.js" defer></script>
  </body>
</html>
